﻿using System;
namespace ShapesConsole
{
    //形状的接口定义
    public interface Shapes
    {
        double getArea();
        bool isLegal();

    }

    //矩形类
    public class Rectangle : Shapes
    {
        private double weight, height;
        public Rectangle(double weight, double height)
        {
            this.weight = weight;
            this.height = height;
        }
        public double Weight
        {
            get { return weight; }
            set { weight = value; }
        }

        public double Height
        {
            get { return height; }
            set { height = value; }

        }
        public double getArea()
        {
            return weight * height;
        }

        public bool isLegal()
        {
            return (weight == 0 && height == 0);
        }
    }

    //正方形类
    public class Square : Shapes
    {
        private double side;
        public Square(double side)
        {
            this.side = side;
        }
        public double Side
        {
            get { return side; }
            set { side = value; }
        }
        public double getArea()
        {
            return side * side;
        }

        public bool isLegal()
        {
            return side > 0;
        }
    }

    //三角形类
    public class Triangle : Shapes
    {
        double side1, side2, side3;
        public Triangle(double side1, double side2, double side3)
        {
            this.side1 = side1;
            this.side2 = side2;
            this.side3 = side3;
        }
        public double getArea()
        {
            double p = side1 + side2 + side3;
            return Math.Sqrt(p * (p - side1) * (p - side2) * (p - side3));
        }

        public bool isLegal()
        {
            return (side1 > 0 && side2 > 0 && side3 > 0);
        }
    }

    public class ShapeFactory
    {
        public static Shapes GetShapes(string type)
        {
            Shapes? shape;
            shape = null;
            if (type.Equals("Rectangle"))
            {
                Random random = new Random();
                double weight = double.Parse((10 * random.NextDouble()).ToString("0.00"));
                double height = double.Parse((10 * random.NextDouble()).ToString("0.00"));

                shape = new Rectangle(weight, height);
                Console.WriteLine("生成长为"+weight+"宽为"+height+"的长方形");
                Console.WriteLine("长方形的面积为："+ (shape.getArea()).ToString("0.00"));
            }
            else if (type.Equals("Square"))
            {
                Random random = new Random();
                double side = double.Parse((10 * random.NextDouble()).ToString("0.00"));
                shape = new Square(side);
                Console.WriteLine("生成边长为"+side+"的正方形");
                Console.WriteLine("正方形的面积为：" + (shape.getArea()).ToString("0.00"));
            }
            else if (type.Equals("Triangle"))
            {
                Random random = new Random();
                double side1 = double.Parse((10 * random.NextDouble()).ToString("0.00"));
                double side2 = double.Parse((10 * random.NextDouble()).ToString("0.00"));
                double side3 = double.Parse((10 * random.NextDouble()).ToString("0.00"));
                shape = new Triangle(side1, side2, side3);
                Console.WriteLine("生成三边长分别为"+side1+" "+side2+" "+side3+"的三角形");
                Console.WriteLine("三方形的面积为：" + (shape.getArea()).ToString("0.00"));
            }
            else
            {
                Console.WriteLine("UnsupportShapeException!");
            }
            Console.WriteLine();
            return shape;
        }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            string[] shapes = new string[3];
            shapes[0] = "Rectangle";
            shapes[1] = "Square";
            shapes[2] = "Triangle";
            Console.WriteLine("随机通过简单工厂生成10个形状：");
            Console.WriteLine();
            for(int i = 0; i < 10; i++)
            {
                Random random = new Random();
                int index = Math.Abs(random.Next() + i * random.Next()) % 3;
                Shapes shape = ShapeFactory.GetShapes(shapes[index]);
            }
        }
    }
}
